﻿Imports CrystalDecisions.CrystalReports.Engine
Imports System.Data.SqlClient

Public Class frmClientProcutionStatement

    Private Sub txtClientNo_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtClientNo.KeyUp
        If Me.txtClientNo.Text.Trim.Length = 0 Then
            Exit Sub
        ElseIf e.KeyCode = Keys.Enter Then
            Try
                Me.Cursor = Cursors.Default

                Me.txtClientName.Text = GetClientName(CInt(Me.txtClientNo.Text.Trim))

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub txtClientNo_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtClientNo.TextChanged
        Me.txtClientName.Clear()
        Me.CrystalReportViewer1.ReportSource = Nothing
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Me.ErrProv.Clear()
        If Me.txtClientName.Text.Trim.Length = 0 Then
            Me.ErrProv.SetError(Me.txtClientNo, "Please fill in all details")
            Me.txtClientNo.Focus()

        Else
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim ColDate, ColName As String
                If Me.RIssueDate.Checked = True Then
                    ColDate = "IssueDate"
                    ColName = "Issue Date"

                ElseIf Me.RInceptionDate.Checked = True Then
                    ColDate = "PeriodFrom"
                    ColName = "Inception Date"

                ElseIf Me.RTransDate.Checked = True Then
                    ColDate = "TransDate"
                    ColName = "Trans. Date (Entry Date)"
                End If

                Dim dap As New SqlDataAdapter("Select * From Policies Where ClientNo=" & Me.txtClientNo.Text.Trim & _
                                              " and " & ColDate & ">= N'" & Me.DTPPeriodFRm.Value.ToShortDateString & " 00:00:00'" & _
                                              " and " & ColDate & "<= N'" & Me.DTPPeriodTo.Value.ToShortDateString & " 23:59:59'" & _
                                              " Order By " & ColDate, cnn)
                Dim das As New DataSet

                dap.SelectCommand.CommandTimeout = 6000
                dap.Fill(das, "Policies")

                Dim rpt As New ClientProductionStatement
                rpt.SetDataSource(das)

                'Create Parameters

                Dim crParameterDiscreteValue As New CrystalDecisions.Shared.ParameterDiscreteValue
                Dim crParameterFieldDefinitions As ParameterFieldDefinitions
                Dim crParameterFieldLocation As ParameterFieldDefinition
                Dim crParameterValues As New CrystalDecisions.Shared.ParameterValues
                crParameterFieldDefinitions = rpt.DataDefinition.ParameterFields


                'ColDate PARAMETER
                crParameterFieldLocation = crParameterFieldDefinitions.Item("ColDate")
                crParameterValues = crParameterFieldLocation.CurrentValues
                crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
                crParameterDiscreteValue.Value = ColName
                crParameterValues.Add(crParameterDiscreteValue)
                crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

                'MinDate PARAMETER
                crParameterFieldLocation = crParameterFieldDefinitions.Item("MinDate")
                crParameterValues = crParameterFieldLocation.CurrentValues
                crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
                crParameterDiscreteValue.Value = Me.DTPPeriodFRm.Value
                crParameterValues.Add(crParameterDiscreteValue)
                crParameterFieldLocation.ApplyCurrentValues(crParameterValues)


                'MaxDate PARAMETER
                crParameterFieldLocation = crParameterFieldDefinitions.Item("MaxDate")
                crParameterValues = crParameterFieldLocation.CurrentValues
                crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
                crParameterDiscreteValue.Value = Me.DTPPeriodTo.Value
                crParameterValues.Add(crParameterDiscreteValue)
                crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

                Me.CrystalReportViewer1.ReportSource = rpt

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Dim a As New frmSearchCustID
        a.ShowDialog()

        If SelClientID = 0 Then
            Exit Sub
        End If

        Me.txtClientNo.Text = SelClientID
        Me.txtClientName.Text = SelClientName
    End Sub

    Private Sub frmClientProcutionStatement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
    End Sub
End Class